﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LantinxShare.LBS;
using LantixLBSServer.Web.DbAccess.Helper;
using System.Data;

namespace LantixLBSServer.Web.DbAccess
{
    public class WorkItemTypeManager : IManager
    {
        /// <summary>
        /// Get All users
        /// </summary>
        /// <returns></returns>
        public List<GWorkItemType> GetGWorkItemTypes()
        {
            List<GWorkItemType> list = new List<GWorkItemType>();

            SqliteDB db = this.GetDB();
            Execute(() =>
            {
                IDataReader reader = db.ExecuteDataReader(@"select id,WorkItemName,WorkType,WorkLevel from t_workitem_type ", false);
                while (reader.Read())
                {
                    GWorkItemType user = new GWorkItemType()
                    {

                        Id = reader.GetInt32(0),
                        WorkItemName = reader.GetString(1),
                        WorkType = reader.GetInt32(2),
                        WorkLevel = reader.GetInt32(3)
                    };
                    list.Add(user);
                }
            }, () => { db.CloseConnection(); });


            return list;
        }


        public bool UpdateGWorkItemType(GWorkItemType workItemType)
        {
            bool result = false;
            SqliteDB db = this.GetDB();

            Execute(() =>
            {
                db.AddParameter("@WorkItemName");
                db.AddParameter("@WorkType");
                db.AddParameter("@WorkLevel");

                db.AddParameter("@id");
                db.SetParameterValue("@WorkItemName", workItemType.WorkItemName);
                db.SetParameterValue("@WorkType", workItemType.WorkType);
                db.SetParameterValue("@WorkLevel", workItemType.WorkLevel);

                db.SetParameterValue("@id", workItemType.Id);

                if (db.ExecuteNonQuery(@"update t_workitem_type set WorkItemName=@WorkItemName,WorkType=@WorkType ,WorkLevel=@WorkLevel where id=@id ", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }

        public bool InsertGWorkItemType(String taskName, String worktype, String worklevel)
        {
            bool result = false;
            SqliteDB db = this.GetDB();

            Execute(() =>
            {
                db.AddParameter("@WorkItemName");
                db.AddParameter("@WorkType");
                db.AddParameter("@WorkLevel");

                db.SetParameterValue("@WorkItemName", taskName);
                db.SetParameterValue("@WorkType", worktype);
                db.SetParameterValue("@WorkLevel", worklevel);

                if (db.ExecuteNonQuery(@"insert into t_workitem_type(WorkItemName,WorkType,WorkLevel) values(@WorkItemName,@WorkType,@WorkLevel)", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }

        public bool DeleteGWorkItemType(int id)
        {
            bool result = false;
            SqliteDB db = this.GetDB();
            Execute(() =>
            {
                db.AddParameter("@id");
                db.SetParameterValue("@id", id);

                if (db.ExecuteNonQuery(@"delete from t_workitem_type where id=@id ", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }
    }
}